package xyz.esion.povertyalleviation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.esion.povertyalleviation.entry.*;
import xyz.esion.povertyalleviation.service.*;

/**
 * @author Esion
 * @since 2021/5/6
 */
@RestController
@RequestMapping("api/industryPool")
public class IndustryPoolController {

    private final VIndustryPoolService serviceV;
    private final TIndustryPoolService service;

    @Autowired
    public IndustryPoolController(VIndustryPoolService serviceV, TIndustryPoolService service) {
        this.serviceV = serviceV;
        this.service = service;
    }

    @GetMapping
    public R list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "9") Integer size
    ){
        // 公共查询
        Page<VIndustryPool> pages = serviceV.page(new Page<>(page, size));
        return R.success().data("items", pages.getRecords()).data("total", pages.getTotal());
    }

    @GetMapping("{id}")
    public R info(@PathVariable("id") String id){
        return R.success().data("item", serviceV.getById(id));
    }

    @PostMapping
    public R add(@RequestBody TIndustryPool entity){
        return R.choose(service.save(entity));
    }

    @PutMapping("{id}")
    public R update(@PathVariable("id") String id, @RequestBody TIndustryPool entity){
        entity.setId(id);
        return R.choose(service.updateById(entity));
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable("id") String id){
        return R.success().data("item", service.removeById(id));
    }


}
